#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
LL hash_table[256]={0};
LL INF=(1LL<<63)-1LL;
void init1()
{
    for (char c='0';c<='9' ;++c )
    {
        hash_table[c]=c-'0';
    }
    for (char c='a';c<='z' ;++c )
    {
        hash_table[c]=c-'a'+10;
    }
}
LL str2Num10(char str[],LL radix,LL upper)
{
    LL ans=0;
    LL len=strlen(str);
    for (LL i=0;i<len ;++i )
    {
        ans=ans*radix+hash_table[str[i]];
        if (ans<0||ans>upper)// long long overflow or greater than N1 .
        {
            return -1;
        }
    }
    return ans;
}
LL cmp(char str[],LL radix,LL upper)
{
    LL len=strlen(str);
    LL num=str2Num10(str,radix,upper);
    if (num<0)//N2>N1
    {
        return 1;
    }
    if (upper>num)//N2<N1
    {
        return -1;
    }
    else if (upper==num)//N2==N1
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
LL binary_find(char N2[],LL left,LL right,LL upper)
{
    LL mid;
    while (left<=right)
    {
        mid=left+(right-left)/2;
        LL flag=cmp(N2,mid,upper);
        if (flag==0)
        {
            return mid;
        }
        else if (flag<0)
        {
            left=mid+1;
        }
        else
        {
            right=mid-1;
        }
    }
    return -1;
}
LL findLargestDigit(char N2[])
{
    LL ans=-1,len=strlen(N2);
    for (LL i=0;i<len ;++i )
    {
        if (hash_table[N2[i]]>ans)
        {
            ans=hash_table[N2[i]];
        }
    }
    return ans+1;
}
int main_1010_2()
{
    init1();
    char N1[11]={0},N2[11]={0};
    LL tag,radix;
    scanf("%s%s%lld%lld",N1,N2,&tag,&radix);
    if (tag==2)
    {
        swap(N1,N2);
    }
    LL upper=str2Num10(N1,radix,INF);
    LL low=findLargestDigit(N2);
    LL high=max(upper,low)+1;
    LL ans=binary_find(N2,low,high,upper);
    if (ans==-1)
    {
        printf("Impossible");
    }
    else
    {
        printf("%lld",ans);
    }
    return 0;
}
